package com.atguigu.educenter.controller;


import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.Response;
import com.atguigu.commonutils.ordervo.UcenterMemberOrder;
import com.atguigu.educenter.entity.UcenterMember;
import com.atguigu.educenter.entity.vo.Registervo;
import com.atguigu.educenter.service.UcenterMemberService;
import com.baomidou.mybatisplus.extension.api.R;
import com.sun.corba.se.spi.presentation.rmi.IDLNameTranslator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.catalina.User;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.jar.JarEntry;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author zhaolie
 * @since 2020-12-18
 */
@RestController
@RequestMapping("/educenter/ucenter")
@Api(description = "用户中心")
public class UcenterMemberController {

    @Resource
    private UcenterMemberService memberService;

    @PostMapping("/login")
    @ApiOperation("用户登录")
    private Response Login(@RequestBody UcenterMember member) {
        // 登录后返回token值
        String token = memberService.login(member);
        return Response.SUCCESS().data("token", token);
    }


    @PostMapping("/register")
    @ApiOperation("用户注册")
    private Response Register(@RequestBody Registervo rvo) {
        // 注册用户
        memberService.register(rvo);
        return Response.SUCCESS();
    }


    @GetMapping("getMemberInfo")
    @ApiOperation("根据token返回用户信息")
    private Response getMemberInfo(HttpServletRequest request) {
        // 调用JWT返回用户id
        String menberId = JwtUtils.getMemberIdByJwtToken(request);
        // 根据用户id返回用户信息
        UcenterMember user = memberService.getUserInfo(menberId);
        return Response.SUCCESS().data("user", user);
    }

    @GetMapping("/{userId}")
    @ApiOperation("根据用户id返回用户信息")
    public UcenterMemberOrder getUserInfo(@PathVariable("userId") String userId) {
        UcenterMember member = memberService.getUserInfo(userId);
        UcenterMemberOrder ucenterMemberOrder = new UcenterMemberOrder();
        BeanUtils.copyProperties(member, ucenterMemberOrder);
        return ucenterMemberOrder;
    }

    @ApiOperation("查询每一天的注册人数")
    @GetMapping("countRegister/{day}")
    public Integer countRegister(@PathVariable("day") String day) {
        return memberService.countRegister(day);
    }
}

